package com.qf.job;

import com.alipay.api.AlipayApiException;
import com.qf.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: chen
 * @Date: 2021/3/24 3:16 PM
 * @Version 1.0
 */
@Component
public class OrderJob {

    public static final String CLEARORDER = "clearOrder";

    @Autowired
    private IOrderService orderService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 清理15分钟还未支付的订单
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void clearOrder() throws AlipayApiException {

        Object clearOrder = redisTemplate.opsForValue().get(CLEARORDER);
        if(clearOrder != null){
            return;
        }
        redisTemplate.opsForValue().set(CLEARORDER,UUID.randomUUID().toString(),6, TimeUnit.MINUTES);
        orderService.clearOrder();
    }

}
